{%- if storeConfig.reddit_pixel_config -%}
  {% assign reddit_pixels = storeConfig.reddit_pixel_config | strtoarray %}
  {% get_pixels_domain list={reddit_pixels} %}

  {% if list %}
    <!-- Reddit Pixel -->

    <script>
        !function(w, d) {
          if (!w.rdt) {
              var p = w.rdt = function() {
                  p.sendEvent ? p.sendEvent.apply(p, arguments) : p.callQueue.push(arguments);
              };
              p.callQueue = [];
              var t = d.createElement("script");
              t.src = "https://www.redditstatic.com/ads/pixel.js";
              t.async = true;
              var s = d.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(t, s);
          }
      }(window, document);

      {%- for pixel in list -%}
        rdt('init', '{{ pixel.pixel_id }}');
      {%- endfor -%}

      rdt('track', 'PageVisit');
    </script>

    <script>
  
      document.addEventListener('DOMContentLoaded', function () {
        let  reddit_pixels = {{ list | json }};
        let reddit_pixel_id = reddit_pixels.map(item => item.pixel_id);
        function pushRedditPixel(event,params = {}) {
          reddit_pixel_id.forEach(id => {
            rdt('track', event,params);
          });
        }

        window['TOP_GLOBAL_CALLBACK_LAYER'].register({
          view_content: function () {
            pushRedditPixel('ViewContent');
          },

          search: function () {
            pushRedditPixel('Search',{
              search_string: '{{search.keywords | filter_string_format}}',
              contents: _GET_C_SETTING_('search_products').map(item => ({
                id: item.spu,
                name: item.title
              }))
            });
          },

     

            purchase: function () {
              {%- if controller == 'order' and action == 'success' -%}
              {% capture pixels %}reddit_pixels_{{order.id}}{% endcapture %}
              {% get_cache key={pixels} var_name=reddit_pixels_status ttl=2592000 %}
              {% if reddit_pixels_status == 0 %}
              pushRedditPixel('Purchase',{
                currency: _GET_C_SETTING_('cart').currency_code,
                {% if storeConfig.reddit_sales_goods_amount %}
                value: "{{ order.current_subtotal_price| plus: order.current_coupon_price| plus: order.current_promotion_price }}",
                {% else %}
                value: "{{ order.total_price }}",
                {% endif %}
                transaction_id: _GET_C_SETTING_('cart').order_number,
                contents: _GET_C_SETTING_('cart').products.map(item => ({
                  id: item.spu,
                  quantity: item.quantity,
                  price: item.price
                })),
                conversion_id: _GET_C_SETTING_('cart').order_number
              
              });
            {% endif %}
          {% endif %}

            },

   

      

          add_to_wishlist: function () {
            pushRedditPixel('AddToWishlist');
          },

          add_to_cart: function (ecdata) {
            pushRedditPixel('AddToCart',{
              currency: _GET_C_SETTING_('base_currency_code'),
              value: ecdata['ec_data_price'] * ecdata['ec_data_quantity'],
              contents: [{
                id: ecdata['ec_data_spu'],
                name: ecdata['ec_data_name'],
                quantity: ecdata['ec_data_quantity'],
                price: ecdata['ec_data_price']
              }]
            });
          },

          sign_up: function () {
            pushRedditPixel('SignUp');
          },
          generate_lead:function(){
            pushRedditPixel('Lead');
          }
        });
      });
    </script>

    {%- for pixel in list -%}
      <noscript>
        <img
          height='1'
          width='1'
          src='https://www.redditstatic.com/ads/pixel.js?id="{{ pixel.pixel_id }}"&ev=pageview&noscript=1'>
      </noscript>
    {%- endfor -%}
  {% endif %}
{%- endif -%}
